package com.lagou.jdbc01;

import java.sql.*;

/**
 * @Author wsl
 * @Date 2021/8/18 20:31
 * @Project_Name mysql
 */
public class JdbcDemo03 {

    public static void main(String[] args) {

        ResultSet resultSet = null;
        Connection con = null;
        Statement statement = null;

        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            String url = "jdbc:mysql://localhost:3306/db4?characterEncoding = UTF-8";
            con = DriverManager.getConnection(url, "root", "123456");

            //3.获取statement对象
            statement = con.createStatement();
            //4.执行查询操作，使用executeQuery方法
            String sql = "select * from jdbc_user";
            //返回结果集对象
            resultSet = statement.executeQuery(sql);
            //处理结果集对象 resultSet

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (null != resultSet){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != statement){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (null != con){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
